package com.open.capacity.server.service;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;


/**
 * @author owen 624191343@qq.com
 * @version 创建时间：2017年11月12日 上午22:57:51
 * 类说明
 * 将oauth_client_details表数据缓存到redis，这里做个缓存优化
 * layui模块中有对oauth_client_details的crud， 注意同步redis的数据
 * 注意对oauth_client_details清楚redis db部分数据的清空
 */

public class ExtClientDetailsService extends JdbcClientDetailsService {


    // 扩展 默认的 ClientDetailsService, 增加逻辑删除判断( status = 1)
    private static String SELECT_CLIENT_DETAILS_SQL = "select client_id, client_secret, resource_ids, scope, authorized_grant_types,  " +
            "            web_server_redirect_uri, authorities, access_token_validity, refresh_token_validity, additional_information, autoapprove  " +
            "            from %soauth_client_details where client_id = ? and status = 1 ";


    private static String SELECT_FIND_STATEMENT = "select client_id, client_secret,resource_ids, scope, " +
            "            + authorized_grant_types, web_server_redirect_uri, authorities, access_token_validity, " +
            "            + refresh_token_validity, additional_information, autoapprove  from %soauth_client_details where status = 1 order by client_id ";
    /**
     * 缓存client的redis key，这里是hash结构存储
     */
    private static final String CACHE_CLIENT_KEY = "oauth_client_details";

    private Logger logger = LoggerFactory.getLogger(ExtClientDetailsService.class);

    public ExtClientDetailsService(DataSource dataSource, String mapper_locations) {
        super(dataSource);
        String pre = "";
        if (mapper_locations.contains("dm")) { //达梦模式
            pre = "\"FINANCE\".";
        } else if (mapper_locations.contains("kingbase")) { //人大金仓模式
            pre = "\"PUBLIC\".";
        }
        setSelectClientDetailsSql(String.format(SELECT_CLIENT_DETAILS_SQL, pre));
        setFindClientDetailsSql(String.format(SELECT_FIND_STATEMENT, pre));
    }


}
